﻿using System.Linq;
using System.Text;
using System;
using System.Xml;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Scurvy;
using Scurvy.Media;
using Scurvy.Media.VideoModel;
using Scurvy.Media.Audio;

namespace TheGame
{    
    //Per ogni file XML generato dall'editor crea un'istanza della classe Level
    //Tutti i livelli sono contenuti in lvlList
    //[p.54]
    class LevelMaker
    {
        #region VARS
        Game1 game;
        XmlDocument doc;
        DirectoryInfo di;
        FileInfo[] fi;
        DirectoryInfo diVid;
        FileInfo[] fiVid;

        XmlAttributeCollection attrList;
        XmlNode root;
        XmlNodeList nodeList;
        XmlNodeList AppigliDestri;
        XmlNodeList AppigliSinistri;
        XmlNodeList BlocchiDestri;
        XmlNodeList BlocchiSinistri;
        XmlNodeList HoistDestri;
        XmlNodeList HoistSinistri;
        XmlNodeList Godowns;
        XmlNodeList LEVE;
        XmlNodeList MATTATOIOList;
        XmlNodeList BOTOLAList;
        XmlNodeList BOMBList;
        XmlNodeList ELECTROList;
        XmlNodeList PORTALList;
        XmlNodeList DOORList;
        XmlNodeList BOMBINNESCOList;
        XmlNodeList GODOWNDList;
        XmlNodeList NODRAWList;
        XmlNodeList Grayareas;
        XmlNodeList Blackareas;

        XmlNodeList Objects;

        XmlNodeList ObjectsA;

        XmlNodeList SpawSligs;
        XmlNodeList SpawMudokons;

        XmlNodeList Floor2nd;


        XmlNodeList LivelliVicini;


        List<string> lvlNameList;
        public static List<Level> lvlList;

        List<Video> videos = new List<Video>();
        List<string> videosName = new List<string>();
        List<Rectangle> videosRec = new List<Rectangle>();
        ContentManager VideoContent; 
        #endregion
       
        public LevelMaker(Game1 game)
        {
            VideoContent = new VideoContentManager(game.Services);
            VideoContent.RootDirectory = "Content";
            //recList = new List<Rectangle>();
            this.game = game;
            di = new DirectoryInfo(@"..\..\..\Content\LVL\XML\");
            fi = di.GetFiles();
            lvlNameList = new List<string>();
            lvlList = new List<Level>();

            diVid = new DirectoryInfo(@"..\..\..\Content\VIDEOS\");
            fiVid = diVid.GetFiles();

            string Nametmp;

            foreach (FileInfo f in fiVid)
            {
                if (f.Extension == ".avi")
                {
                    Nametmp = f.Name.Remove(f.Name.Length - 4, 4);
                    if (Nametmp.Split('_').Count() == 5)
                    {
                        videos.Add(VideoContent.Load<Video>("VIDEOS\\" + Nametmp));
                        videosName.Add(Nametmp.Split('_')[0]);
                        videosRec.Add(new Rectangle(int.Parse(Nametmp.Split('_')[1]), int.Parse(Nametmp.Split('_')[2]), int.Parse(Nametmp.Split('_')[3]), int.Parse(Nametmp.Split('_')[4])));
                    }
                }
            }
            Level level;

            foreach (FileInfo f in fi)
            {
                level = makeLVL(f.Name.Remove(f.Name.Length - 4));
                if(level != null)
                    lvlList.Add(level);
            }
           
            game.CurrentLevel = lvlList[0];

            #region DEMO
            ////////////////////////////////////////////////////////////////
            Level la;
            Level lb;



            lb = new Level(getLevel("R1P15C02"));
            lvlList[0].NextRIGHT = lb;
            lb.NextLEFT = lvlList[0];

            la = lb;
            lb = new Level(getLevel("R1P16C03"));
            game.REPORT.LEVELS.Add(la);
            la.NextRIGHT = lb;
            lb.NextLEFT = la;

            la = lb;
            lb = new Level(getLevel("R1P16C06"));
            game.REPORT.LEVELS.Add(la);
            la.NextRIGHT = lb;
            lb.NextLEFT = la;

            la = lb;
            lb = new Level(getLevel("R1P18C02"));
            game.REPORT.LEVELS.Add(la);
            la.NextRIGHT = lb;
            lb.NextLEFT = la;

            la = lb;
            lb = new Level(getLevel("R1P18C11"));
            game.REPORT.LEVELS.Add(la);
            la.NextRIGHT = lb;
            lb.NextLEFT = la;

            la = lb;
            lb = new Level(getLevel("R1P18C03"));
            game.REPORT.LEVELS.Add(la);
            la.NextRIGHT = lb;
            lb.NextLEFT = la;

            la = lb;
            lb = new Level(getLevel("R1P16C02"));
            game.REPORT.LEVELS.Add(la);
            la.NextRIGHT = lb;
            lb.NextLEFT = la;



            /////////////////////////////////////////////////////////////////////////////////////// 
            #endregion

        }

        //Viene navigata la struttura di ogni file XML e creato il livello corrispondente
        public Level makeLVL(string lvlName)
        {
            doc = new XmlDocument();
            doc.Load(@"..\..\..\Content\LVL\XML\" + lvlName + ".xml");
            root = doc.DocumentElement;
            nodeList = root.ChildNodes;

            Level lvl = null;

            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].Name == "ENVIOREMENT")
                {
                    attrList = nodeList[i].Attributes;
                    string ENV = attrList["NAME"].Value;
                    lvl = new Level(ENV, game);
                }

                if (nodeList[i].Name == "NAME")
                {
                    attrList = nodeList[i].Attributes;
                    string Name = attrList["NAME"].Value;
                    lvl.NAME = Name;
                }
                if (nodeList[i].Name == "FLOOR2NDLEFT")
                {
                    attrList = nodeList[i].Attributes;
                    string Val = attrList["SET"].Value;
                    if (Val == "Y")
                        lvl.FLOOR2NDLeft = true;
                    else
                        lvl.FLOOR2NDLeft = false;
                }
                if (nodeList[i].Name == "FLOOR2NDRIGHT")
                {
                    attrList = nodeList[i].Attributes;
                    string Val = attrList["SET"].Value;
                    if (Val == "Y")
                        lvl.FLOOR2NDRight = true;
                    else
                        lvl.FLOOR2NDRight = false;
                }
                if (nodeList[i].Name == "BLOCCOUPRIGHT")
                {
                    attrList = nodeList[i].Attributes;
                    string Val = attrList["SET"].Value;
                    if (Val == "Y")
                        lvl.BLOCCOUPRight = true;
                    else
                        lvl.BLOCCOUPRight = false;
                }
                if (nodeList[i].Name == "BLOCCOUPLEFT")
                {
                    attrList = nodeList[i].Attributes;
                    string Val = attrList["SET"].Value;
                    if (Val == "Y")
                        lvl.BLOCCOUPLeft = true;
                    else
                        lvl.BLOCCOUPLeft = false;
                }
                if (nodeList[i].Name == "BLOCCODOWNLEFT")
                {
                    attrList = nodeList[i].Attributes;
                    string Val = attrList["SET"].Value;
                    if (Val == "Y")
                        lvl.BLOCCODOWNLeft = true;
                    else
                        lvl.BLOCCODOWNLeft = false;
                }
                if (nodeList[i].Name == "BLOCCODOWNRIGHT")
                {
                    attrList = nodeList[i].Attributes;
                    string Val = attrList["SET"].Value;
                    if (Val == "Y")
                        lvl.BLOCCODOWNRight = true;
                    else
                        lvl.BLOCCODOWNRight = false;
                }
                if (nodeList[i].Name == "GODOWNLIST")
                {
                    Godowns = nodeList[i].ChildNodes;
                    for (int k = 0; k < Godowns.Count; k++)
                    {
                        if (Godowns[k].Name == "GODOWN")
                        {
                            attrList = Godowns[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            lvl.AddCOLLISION(new Collision(COLLISIONS.GODOWN, rec));
                        }
                    }
                }
                if (nodeList[i].Name == "HOISTLIST")
                {
                    HoistDestri = nodeList[i].ChildNodes;
                    for (int k = 0; k < HoistDestri.Count; k++)
                    {
                        if (HoistDestri[k].Name == "HOIST")
                        {
                            attrList = HoistDestri[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            string isAdvise = attrList["ADVISE"].Value;

                            if (isAdvise == "Y")
                            {
                                ObjectA Advise = new ObjectA(game, "HOISTADVISE", new Rectangle(rec.X, rec.Y, rec.Width, 480), new Collision("", new Rectangle()), lvl);
                                lvl.AddObjectA(Advise);
                            }
                            if (IntersectsGodown(new Rectangle(rec.X - 40, rec.Y, rec.Width, rec.Height), lvl))
                                lvl.AddCOLLISION(new Collision(COLLISIONS.HOISTDESTRO, rec));
                            else
                                lvl.AddCOLLISION(new Collision(COLLISIONS.HOISTSINISTRO, rec));
                        }
                    }
                }
                if (nodeList[i].Name == "APPIGLIOLIST")
                {
                    AppigliDestri = nodeList[i].ChildNodes;
                    for (int k = 0; k < AppigliDestri.Count; k++)
                    {
                        if (AppigliDestri[k].Name == "APPIGLIO")
                        {
                            attrList = AppigliDestri[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            if (IntersectsGodown(new Rectangle(rec.X - 40, rec.Y - 200, rec.Width, rec.Height), lvl))
                                lvl.AddCOLLISION(new Collision(COLLISIONS.APPIGLIODESTRO, rec));
                            else
                                lvl.AddCOLLISION(new Collision(COLLISIONS.APPIGLIOSINISTRO, rec));
                        }
                    }
                }
                if (nodeList[i].Name == "BLOCCOLIST")
                {
                    BlocchiDestri = nodeList[i].ChildNodes;
                    for (int k = 0; k < BlocchiDestri.Count; k++)
                    {
                        if (BlocchiDestri[k].Name == "BLOCCO")
                        {
                            attrList = BlocchiDestri[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            lvl.AddCOLLISION(new Collision(COLLISIONS.BLOCCO, rec));
                        }
                    }
                }
                if (nodeList[i].Name == "DOORLIST")
                {
                    DOORList = nodeList[i].ChildNodes;
                    for (int k = 0; k < DOORList.Count; k++)
                    {
                        if (DOORList[k].Name == "DOOR")
                        {
                            attrList = DOORList[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            lvl.AddCOLLISION(new Collision(COLLISIONS.DOOR, rec));
                        }
                    }
                }

                if (nodeList[i].Name == "PORTALLIST")
                {
                    PORTALList = nodeList[i].ChildNodes;

                    for (int k = 0; k < PORTALList.Count; k++)
                    {
                        if (PORTALList[k].Name == "PORTAL")
                        {
                            attrList = PORTALList[k].Attributes;
                            Rectangle PortalRec = new Rectangle();
                            PortalRec.X = toInt(attrList["X"].Value);
                            PortalRec.Y = toInt(attrList["Y"].Value);
                            PortalRec.Width = toInt(attrList["WIDTH"].Value);
                            PortalRec.Height = toInt(attrList["HEIGHT"].Value);

                            Collision PortaColl = new Collision(COLLISIONS.PORTAL, PortalRec);
                            ObjectA portal = new ObjectA(game, "PORTAL", PortalRec, PortaColl, lvl);
                            PortaColl.OBJA = portal;
                            lvl.AddObjectA(portal);
                            lvl.AddCOLLISION(PortaColl);
                        }
                    }
                }
                if (nodeList[i].Name == "ELECTROLIST")
                {
                    XmlNodeList ElectroChilds;
                    XmlNodeList AzionatoreChilds;
                    ObjectA Leva = null;
                    ELECTROList = nodeList[i].ChildNodes;

                    for (int k = 0; k < ELECTROList.Count; k++)
                    {
                        if (ELECTROList[k].Name == "ELECTRO")
                        {
                            attrList = ELECTROList[k].Attributes;
                            Rectangle CollisionRecE = new Rectangle();
                            CollisionRecE.X = toInt(attrList["X"].Value);
                            CollisionRecE.Y = toInt(attrList["Y"].Value);
                            CollisionRecE.Width = toInt(attrList["WIDTH"].Value);
                            CollisionRecE.Height = toInt(attrList["HEIGHT"].Value);

                            ElectroChilds = ELECTROList[k].ChildNodes;

                            attrList = ElectroChilds[0].Attributes;
                            Rectangle DrawingRecE = new Rectangle();
                            DrawingRecE.X = toInt(attrList["X"].Value);
                            DrawingRecE.Y = toInt(attrList["Y"].Value);
                            DrawingRecE.Width = toInt(attrList["WIDTH"].Value);
                            DrawingRecE.Height = toInt(attrList["HEIGHT"].Value);

                            ObjectA Electro = new ObjectA(game, "ELECTRO", DrawingRecE, new Collision(COLLISIONS.ELECTRO, CollisionRecE), lvl);

                            if (ELECTROList[k].ChildNodes.Count > 1)
                            {
                                AzionatoreChilds = ELECTROList[k].ChildNodes[1].ChildNodes;

                                attrList = ELECTROList[k].ChildNodes[1].Attributes;
                                Rectangle CollisionRecA = new Rectangle();
                                CollisionRecA.X = toInt(attrList["X"].Value);
                                CollisionRecA.Y = toInt(attrList["Y"].Value);
                                CollisionRecA.Width = toInt(attrList["WIDTH"].Value);
                                CollisionRecA.Height = toInt(attrList["HEIGHT"].Value);

                                attrList = AzionatoreChilds[0].Attributes;
                                Rectangle DrawingRecA = new Rectangle();
                                DrawingRecA.X = toInt(attrList["X"].Value);
                                DrawingRecA.Y = toInt(attrList["Y"].Value);
                                DrawingRecA.Width = toInt(attrList["WIDTH"].Value);
                                DrawingRecA.Height = toInt(attrList["HEIGHT"].Value);

                                Collision levaColl = new Collision(COLLISIONS.LEVA, CollisionRecA);
                                Leva = new ObjectA(game, "LEVA", DrawingRecA, levaColl, lvl);
                                levaColl.OBJA = Leva;

                                lvl.AddObjectA(Leva);
                                lvl.AddCOLLISION(Leva.COLLISION);
                            }

                            Leva.AddSlave(Electro);
                            lvl.AddObjectA(Electro);
                        }
                    }
                }
                if (nodeList[i].Name == "BOMBLIST")
                {
                    BOMBList = nodeList[i].ChildNodes;

                    for (int k = 0; k < BOMBList.Count; k++)
                    {
                        if (BOMBList[k].Name == "BOMB")
                        {
                            attrList = BOMBList[k].Attributes;
                            Rectangle CollisionRecB = new Rectangle();
                            CollisionRecB.X = toInt(attrList["X"].Value);
                            CollisionRecB.Y = toInt(attrList["Y"].Value);
                            CollisionRecB.Width = toInt(attrList["WIDTH"].Value);
                            CollisionRecB.Height = toInt(attrList["HEIGHT"].Value);


                            attrList = BOMBList[k].FirstChild.Attributes;
                            Rectangle DrawingRecB = new Rectangle();
                            DrawingRecB.X = toInt(attrList["X"].Value);
                            DrawingRecB.Y = toInt(attrList["Y"].Value);
                            DrawingRecB.Width = toInt(attrList["WIDTH"].Value);
                            DrawingRecB.Height = toInt(attrList["HEIGHT"].Value);

                            Collision bombColl = new Collision(COLLISIONS.BOMB, CollisionRecB);
                            ObjectA Bomb = new ObjectA(game, "BOMB", DrawingRecB, bombColl, lvl);
                            bombColl.OBJA = Bomb;

                            lvl.AddObjectA(Bomb);
                        }
                    }
                }

                if (nodeList[i].Name == "BOMBINNESCOLIST")
                {
                    BOMBINNESCOList = nodeList[i].ChildNodes;

                    for (int k = 0; k < BOMBINNESCOList.Count; k++)
                    {
                        if (BOMBINNESCOList[k].Name == "BOMBINNESCO")
                        {
                            attrList = BOMBINNESCOList[k].Attributes;
                            Rectangle CollisionRecB = new Rectangle();
                            CollisionRecB.X = toInt(attrList["X"].Value);
                            CollisionRecB.Y = toInt(attrList["Y"].Value);
                            CollisionRecB.Width = toInt(attrList["WIDTH"].Value);
                            CollisionRecB.Height = toInt(attrList["HEIGHT"].Value);

                            attrList = BOMBINNESCOList[k].FirstChild.Attributes;
                            Rectangle DrawingRecB = new Rectangle();
                            DrawingRecB.X = toInt(attrList["X"].Value);
                            DrawingRecB.Y = toInt(attrList["Y"].Value);
                            DrawingRecB.Width = toInt(attrList["WIDTH"].Value);
                            DrawingRecB.Height = toInt(attrList["HEIGHT"].Value);

                            Collision bombInnColl = new Collision(COLLISIONS.BOMBINNESCO, CollisionRecB);
                            ObjectA BombInnesco = new ObjectA(game, "BOMBINNESCO", DrawingRecB, bombInnColl, lvl);
                            bombInnColl.OBJA = BombInnesco;

                            lvl.AddObjectA(BombInnesco);
                        }
                    }
                }

                if (nodeList[i].Name == "MATTATOIOLIST")
                {
                    XmlNodeList MattatoioChilds;
                    XmlNodeList AzionatoreChilds;

                    MATTATOIOList = nodeList[i].ChildNodes;
                    for (int k = 0; k < MATTATOIOList.Count; k++)
                    {
                        if (MATTATOIOList[k].Name == "MATTATOIO")
                        {
                            attrList = MATTATOIOList[k].Attributes;
                            Rectangle CollisionRecM = new Rectangle();
                            CollisionRecM.X = toInt(attrList["X"].Value);
                            CollisionRecM.Y = toInt(attrList["Y"].Value);
                            CollisionRecM.Width = toInt(attrList["WIDTH"].Value);
                            CollisionRecM.Height = toInt(attrList["HEIGHT"].Value);

                            MattatoioChilds = MATTATOIOList[k].ChildNodes;

                            attrList = MattatoioChilds[0].Attributes;
                            Rectangle DrawingRecM = new Rectangle();
                            DrawingRecM.X = toInt(attrList["X"].Value);
                            DrawingRecM.Y = toInt(attrList["Y"].Value);
                            DrawingRecM.Width = toInt(attrList["WIDTH"].Value);
                            DrawingRecM.Height = toInt(attrList["HEIGHT"].Value);

                            ObjectA Mattatoio = new ObjectA(game, "MATTATOIO", DrawingRecM, new Collision(COLLISIONS.MATTATOIO, CollisionRecM), lvl);

                            AzionatoreChilds = MATTATOIOList[k].ChildNodes[1].ChildNodes;

                            attrList = MATTATOIOList[k].ChildNodes[1].Attributes;
                            Rectangle CollisionRecA = new Rectangle();
                            CollisionRecA.X = toInt(attrList["X"].Value);
                            CollisionRecA.Y = toInt(attrList["Y"].Value);
                            CollisionRecA.Width = toInt(attrList["WIDTH"].Value);
                            CollisionRecA.Height = toInt(attrList["HEIGHT"].Value);

                            attrList = AzionatoreChilds[0].Attributes;
                            Rectangle DrawingRecA = new Rectangle();
                            DrawingRecA.X = toInt(attrList["X"].Value);
                            DrawingRecA.Y = toInt(attrList["Y"].Value);
                            DrawingRecA.Width = toInt(attrList["WIDTH"].Value);
                            DrawingRecA.Height = toInt(attrList["HEIGHT"].Value);

                            Collision levaColl = new Collision(COLLISIONS.LEVA, CollisionRecA);
                            ObjectA Leva = new ObjectA(game, "LEVA", DrawingRecA, levaColl, lvl);
                            levaColl.OBJA = Leva;

                            Leva.AddSlave(Mattatoio);
                            lvl.AddObjectA(Mattatoio);
                            lvl.AddObjectA(Leva);
                            lvl.AddCOLLISION(Leva.COLLISION);
                        }
                    }
                }

                if (nodeList[i].Name == "BOTOLALIST")
                {
                    XmlNodeList BotolaChilds;
                    XmlNodeList AzionatoreChilds;

                    MATTATOIOList = nodeList[i].ChildNodes;
                    BOTOLAList = nodeList[i].ChildNodes;
                    for (int k = 0; k < BOTOLAList.Count; k++)
                    {
                        if (BOTOLAList[k].Name == "BOTOLA")
                        {
                            attrList = BOTOLAList[k].Attributes;
                            Rectangle CollisionRecM = new Rectangle();
                            CollisionRecM.X = toInt(attrList["X"].Value);
                            CollisionRecM.Y = toInt(attrList["Y"].Value);
                            CollisionRecM.Width = toInt(attrList["WIDTH"].Value);
                            CollisionRecM.Height = toInt(attrList["HEIGHT"].Value);

                            BotolaChilds = MATTATOIOList[k].ChildNodes;

                            attrList = BotolaChilds[0].Attributes;
                            Rectangle DrawingRecM = new Rectangle();
                            DrawingRecM.X = toInt(attrList["X"].Value);
                            DrawingRecM.Y = toInt(attrList["Y"].Value);
                            DrawingRecM.Width = toInt(attrList["WIDTH"].Value);
                            DrawingRecM.Height = toInt(attrList["HEIGHT"].Value);

                            ObjectA Botola = new ObjectA(game, "BOTOLA", DrawingRecM, new Collision(COLLISIONS.BOTOLA, CollisionRecM), lvl);

                            AzionatoreChilds = BOTOLAList[k].ChildNodes[1].ChildNodes;

                            attrList = BOTOLAList[k].ChildNodes[1].Attributes;
                            Rectangle CollisionRecA = new Rectangle();
                            CollisionRecA.X = toInt(attrList["X"].Value);
                            CollisionRecA.Y = toInt(attrList["Y"].Value);
                            CollisionRecA.Width = toInt(attrList["WIDTH"].Value);
                            CollisionRecA.Height = toInt(attrList["HEIGHT"].Value);

                            attrList = AzionatoreChilds[0].Attributes;
                            Rectangle DrawingRecA = new Rectangle();
                            DrawingRecA.X = toInt(attrList["X"].Value);
                            DrawingRecA.Y = toInt(attrList["Y"].Value);
                            DrawingRecA.Width = toInt(attrList["WIDTH"].Value);
                            DrawingRecA.Height = toInt(attrList["HEIGHT"].Value);

                            Collision levaColl = new Collision(COLLISIONS.LEVA, CollisionRecA);
                            ObjectA Leva = new ObjectA(game, "LEVA", DrawingRecA, levaColl, lvl);
                            levaColl.OBJA = Leva;

                            Leva.AddSlave(Botola);
                            lvl.AddObjectA(Botola);
                            lvl.AddObjectA(Leva);
                            lvl.AddCOLLISION(Leva.COLLISION);
                        }
                    }
                }

                if (nodeList[i].Name == "GODOWNDLIST")
                {
                    GODOWNDList = nodeList[i].ChildNodes;
                    for (int k = 0; k < GODOWNDList.Count; k++)
                    {
                        if (GODOWNDList[k].Name == "GODOWND")
                        {
                            attrList = GODOWNDList[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            lvl.AddCOLLISION(new Collision(COLLISIONS.GODOWND, rec));
                        }
                    }
                }

                if (nodeList[i].Name == "LEVALIST")
                {
                    LEVE = nodeList[i].ChildNodes;
                    for (int k = 0; k < LEVE.Count; k++)
                    {
                        if (LEVE[k].Name == "LEVA")
                        {
                            attrList = LEVE[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            lvl.AddCOLLISION(new Collision(COLLISIONS.LEVA, rec));
                        }
                    }
                }

                if (nodeList[i].Name == "GRAYLIST")
                {
                    Grayareas = nodeList[i].ChildNodes;
                    for (int k = 0; k < Grayareas.Count; k++)
                    {
                        if (Grayareas[k].Name == "GRAY")
                        {
                            attrList = Grayareas[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            lvl.AddCOLLISION(new Collision(COLLISIONS.GRAYAREA, rec));
                        }
                    }
                }

                if (nodeList[i].Name == "BLACKLIST")
                {
                    Blackareas = nodeList[i].ChildNodes;
                    for (int k = 0; k < Blackareas.Count; k++)
                    {
                        if (Blackareas[k].Name == "BLACK")
                        {
                            attrList = Blackareas[k].Attributes;
                            Rectangle rec = new Rectangle();
                            rec.X = toInt(attrList["X"].Value);
                            rec.Y = toInt(attrList["Y"].Value);
                            rec.Width = toInt(attrList["WIDTH"].Value);
                            rec.Height = toInt(attrList["HEIGHT"].Value);

                            lvl.AddCOLLISION(new Collision(COLLISIONS.BLACKAREA, rec));
                        }
                    }
                }

                if (nodeList[i].Name == "SENTINELLA")
                {

                    string sentinella = nodeList[i].Attributes["SET"].Value;

                    if (sentinella == "Y")
                    {
                        lvl.Sentinella = true;
                        ObjectA OMM = new ObjectA(game, "OMM", new Rectangle(500, 50, 0, 0), new Collision(COLLISIONS.SHOCK, new Rectangle()), lvl);
                        lvl.AddObjectA(OMM);
                    }
                    else
                        lvl.Sentinella = false;
                }

                if (nodeList[i].Name == "SPAWNSLIGLIST")
                {
                    SpawSligs = nodeList[i].ChildNodes;
                    XmlNodeList Sligs;
                    for (int k = 0; k < SpawSligs.Count; k++)
                    {
                        if (SpawSligs[k].Name == "SPAWNSLIG")
                        {
                            attrList = SpawSligs[k].Attributes;
                            Rectangle SpawnRec = new Rectangle();
                            SpawnRec.X = toInt(attrList["X"].Value);
                            SpawnRec.Y = toInt(attrList["Y"].Value);
                            SpawnRec.Width = toInt(attrList["WIDTH"].Value);
                            SpawnRec.Height = toInt(attrList["HEIGHT"].Value);

                            Sligs = SpawSligs[k].ChildNodes;

                            for (int s = 0; s < Sligs.Count; s++)
                            {

                                attrList = Sligs[s].Attributes;
                                Slig slig = new Slig(game, attrList["STATE"].Value, toInt(attrList["AI"].Value), SpawnRec.Location);
                                slig.levelDefault = lvl;
                                lvl.AddSlig(slig);
                            }
                        }
                    }
                }

                if (nodeList[i].Name == "SPAWNMUDOKONLIST")
                {
                    SpawMudokons = nodeList[i].ChildNodes;
                    XmlNodeList Mudokons;
                    for (int k = 0; k < SpawMudokons.Count; k++)
                    {
                        if (SpawMudokons[k].Name == "SPAWNMUDOKON")
                        {
                            attrList = SpawMudokons[k].Attributes;
                            Rectangle SpawnRec = new Rectangle();
                            SpawnRec.X = toInt(attrList["X"].Value);
                            SpawnRec.Y = toInt(attrList["Y"].Value);
                            SpawnRec.Width = toInt(attrList["WIDTH"].Value);
                            SpawnRec.Height = toInt(attrList["HEIGHT"].Value);

                            Mudokons = SpawMudokons[k].ChildNodes;

                            for (int m = 0; m < Mudokons.Count; m++)
                            {
                                attrList = Mudokons[m].Attributes;
                                Mudokon mudokon = new Mudokon(game, attrList["STATE"].Value, SpawnRec.Location);
                                lvl.AddMudokon(mudokon);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < videosName.Count; i++)
            {
                if (videosName[i] == lvl.Enviorement)
                {
                    lvl.AddVideo(videos[i], videosRec[i]);
                }
            }
            lvlNameList.Add(lvlName);

            lvl.SetDifficult();
            return lvl;
        }


        bool IntersectsGodown(Rectangle recToTest, Level lvl)
        {
            for (int i = 0; i < lvl.COLLISIONGROUPS.Count; i++)
            {
                for (int j = 0; j < lvl.COLLISIONGROUPS[i].Count; j++)
                {
                    if (lvl.COLLISIONGROUPS[i][j].TYPE == COLLISIONS.GODOWN)
                    {
                        if (recToTest.Intersects(lvl.COLLISIONGROUPS[i][j].REC))
                            return true;
                    }

                }
            }
            return false;
        } 

        public static Level getLevel(string enviorement)
        {
            foreach (Level lvl in lvlList)
            {
                if (lvl.Enviorement == enviorement)
                    return lvl;
            }

            return null;
        }

        public static Level getLevelByName(string name)
        {
            foreach (Level lvl in lvlList)
            {
                if (lvl.NAME == name)
                    return lvl;
            }

            return null;
        }

        int toInt(String val)
        {
            return int.Parse(val);
        }
    }
}
